<?php
class Db
{
	private $conn;
	private $sql;
	private $result;
	private $addFields;
	private $addValues;
	private $saveForm;
	function __construct(){
		global $config;
		if($this->conn == 'pconn')
			$this->conn = @mysql_pconnect($config['db_host'].":".$config['port'], $config['db_user'], $config['db_passwd']);
		else
			$this->conn = @mysql_connect($config['db_host'].":".$config['port'], $config['db_user'], $config['db_passwd']);
		if(!$this->conn){
			$this->error('连接数据库失败！');
		}
		if(!mysql_select_db($config['db_database'], $this->conn)){
			$this->error('选择数据库失败！');
		}
		mysql_query("SET NAMES " . $config['coding']);
	}
	protected function matchField($tb, $arr = array()){
		$return = array();
		$fields = $this->query("describe $tb");		
		foreach($fields as $field){
			if( $field['Key'] == 'PRI' ) continue;			
			foreach($arr as $k=>$v){
				if( $field['Field'] == $k ){				
					$v = str_replace("'", '"', $v);
					$v = str_replace("‘", '"', $v);		
					$return[$k] = $v; break;
				}
			}
		}
		return $return;
	}	
	protected function creatAddForm($table, $data){
		$this->addFields = '';
		$this->addValues = '';
		if(!is_array($data) || empty($data)) return;
		$i = 0;
		$data = $this->matchField($table, $data);
		foreach($data as $k=>$v){
			$v = addslashes($v);
			if($i==0){
				$this->addFields = "".trim($k)."";
				$this->addValues = "'".trim($v)."'";
			}
			else{
				$this->addFields .= ','."".trim($k)."";
				$this->addValues .= ','."'".trim($v)."'";
			}
			$i++;
		}
	}
	protected function creatSaveForm($table, $data){
		$this->saveForm = '';
		$i = 0;
		$data = $this->matchField($table, $data);
		foreach($data as $k=>$v){
			$v = addslashes($v);
			if($i==0){
				$this->saveForm = $k."='".trim($v)."'";
			}
			else{
				$this->saveForm .= ','.$k."='".trim($v)."'";
			}
			$i++;
		}
	}	
	function query($sql, $one = false){
		$this->sql = $sql;
		$result = @mysql_query($this->sql, $this->conn);
		if(!$result){
			$this->error('获取数据记录失败！');
		}
		$record = array();
		while($row = mysql_fetch_assoc($result)){
			if( $one ){
				$record = $row; break;
			}else{
				$record[] = $row;			
			}
		}
		return $record;
	}
	//获取单条记录
	function find($table,$where,$order = 'id desc'){
		$this->sql = "SELECT * FROM $table where $where order by $order limit 0,1";
		$result = @mysql_query($this->sql, $this->conn);
		if(!$result){
			$this->error('获取数据记录失败！');
		}
		$record = array();
		while($row = mysql_fetch_assoc($result)){
			$record = $row;
		}
		return $record;
	}
	function add($table,$data){
		unset($data['id']);
		$this->creatAddForm($table, $data);		
		if(empty($this->addFields) || empty($this->addValues)) return false;
		$this->sql = "INSERT INTO ".$table."(".$this->addFields.") VALUES(".$this->addValues.")";		
		$result = mysql_query($this->sql);	
		if(!$result) $this->error();
		return $result;
	}
	function save($table,$where,$data){
		$this->creatSaveForm($table, $data);
		if(empty($this->saveForm)) return false;
		$this->sql = "UPDATE $table SET ".$this->saveForm." WHERE $where";		
		$result = mysql_query($this->sql);	
		if(!$result) $this->error();
		return $result;		
	}
	function delete($table,$where = "1=1"){
		$this->sql = "delete from $table where $where";
		$result = @mysql_query($this->sql, $this->conn);
	}
	function insert_id()
	{
		return mysql_insert_id();
	}	 
	function error()
	{
		$return['state'] = 0;
		$return['msg']   = '1、错误信息：'.mysql_error().'<br>2、sql语句：'.$this->sql;		
		echo json_encode($return);		
		exit;
	}
}
?>